package cn.kgc.novel.service.impl;

import cn.kgc.novel.common.Result;
import cn.kgc.novel.mapper.UsersMapper;
import cn.kgc.novel.pojo.Bookshelf;
import cn.kgc.novel.pojo.HistoryBook;
import cn.kgc.novel.pojo.HistoryUser;
import cn.kgc.novel.pojo.Users;
import cn.kgc.novel.service.UsersService;
import com.mongodb.BasicDBObject;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

//杨将
@Service
public class UsersServiceImpl implements UsersService {
    @Resource
    private HistoryBook historyBook;
    @Resource
    private HistoryUser historyUser;
    @Resource
    private Bookshelf bookshelf;
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private UsersMapper usersMapper;
    @Resource
    private RedisTemplate<String,Object> RedisTemplate;
    //<!--    根据数字降序-->
    @Override
    public List<Users> selectx1(String sex, String category, String status, String num) {
        return usersMapper.selectx1(sex, category, status, num);
    }
    //<!--    根据创建时间从新到旧排序-->
    @Override
    public List<Users> selectx2(String sex, String category, String status, String num) {
        return usersMapper.selectx2(sex, category, status, num);
    }
    //<!--    根据热度降序-->
    @Override
    public List<Users> selectx3(String sex, String category, String status, String num) {
        return usersMapper.selectx3(sex, category, status, num);
    }
    //根据小说名查作者
    @Override
    public List<Users> checktheauthor(String bookname) {
        return usersMapper.checktheauthor(bookname);
    }
    //根据作者名查小说
    @Override
    public List<Users> novelauthorname(String name) {
        return usersMapper.novelauthorname(name);
    }
    //查询作者和小说名
    @Override
    public List<Users> flieshow() {
        return usersMapper.flieshow();
    }
    //查询所有user
    @Override
    public List<Users> findList(Users users) {
        return usersMapper.getUsersList(users);
    }
    //通过id查询user
    @Override
    public Users findById(Integer uid) {
        return usersMapper.getById(uid);
    }

    @Override
    public void edit(Users users) {
        usersMapper.update(users);
    }

    @Override
    public void register(Users users) {
        usersMapper.add(users);
    }

    @Override
    public void delete(Integer uid) {
        usersMapper.remove(uid);
    }
    //通过账号查找user
    @Override
    public Users findByAccount(String account) {
        return usersMapper.getByAccount(account);
    }
    //注册mongo
    @Override
    public void regMongo(int uid) {
        historyUser.setId(uid);
        mongoTemplate.save(historyUser);
        addHis(uid,0,0);
        addShelf(uid,0);
    }
    //通过uid,bid来查看历史阅读记录
    @Override
    public int getHistById(int uid, int bid) {
        int cid=0;
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(uid));
        HistoryUser historyUser = mongoTemplate.find(query, HistoryUser.class).get(0);
        for (HistoryBook historyBook:historyUser.getHistoryBooks()) {
            if (historyBook.getBid()==bid){
                cid=historyBook.getCid();
            }
        }
        return cid;
    }
    //添加历史阅读记录
    @Override
    public void addHis(int uid, int bid, int cid) {
        Query query=new Query(Criteria.where("id").is(uid));
        Update update=new Update();
        historyBook.setBid(bid);
        historyBook.setCid(cid);
        update.addToSet("historyBooks",historyBook);
        mongoTemplate.updateFirst(query,update,HistoryUser.class);
    }
    //修改历史阅读记录
    @Override
    public void updateHis(int uid, int bid, int cid) {
        Query query = new Query(Criteria.where("id").is(uid)
                .and("historyBooks")
                .elemMatch(Criteria.where("bid").is(bid))
        );
        Update update=new Update();
        historyBook.setBid(bid);
        historyBook.setCid(cid);
        update.set("historyBooks.$",historyBook);
        mongoTemplate.updateFirst(query,update,HistoryUser.class);
    }
    //查看是否加入书架
    @Override
    public int findShelf(int uid, int bid) {
        int num=0;
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(uid));
        HistoryUser historyUser = mongoTemplate.find(query, HistoryUser.class).get(0);
        //System.out.println(historyUser);
        for (Bookshelf bookshelf:historyUser.getBookshelves()) {
            if (bookshelf.getBid()==bid){
                num=bookshelf.getIsExist();
            }
        }
        return num;
    }
    //加入书架
    @Override
    public void addShelf(int uid, int bid) {
        Query query=new Query(Criteria.where("id").is(uid));
        Update update=new Update();
        bookshelf.setBid(bid);
        bookshelf.setIsExist(1);
        update.addToSet("bookshelves",bookshelf);
        mongoTemplate.updateFirst(query,update,HistoryUser.class);
    }
    //移除书架
    @Override
    public void delShelf(int uid, int bid) {
        Query query = new Query(Criteria.where("id").is(uid));
        Update update = new Update();
        update.pull("bookshelves",new BasicDBObject("bid",bid));
        mongoTemplate.updateFirst(query, update, HistoryUser.class);
    }
    //通过uid来查看书架的书籍
    @Override
    public List<Bookshelf> findShelfById(int uid) {
        List<Bookshelf> bookshelves=new ArrayList<Bookshelf>();
        Query query = new Query(Criteria.where("id").is(uid));
        HistoryUser historyUser = mongoTemplate.find(query, HistoryUser.class).get(0);
        for (Bookshelf bookshelf:historyUser.getBookshelves()) {
            System.out.println(bookshelf);
            if (bookshelf.getBid()!=0){
                bookshelves.add(bookshelf);
            }
        }
        return bookshelves;
    }

    //查询
    @Override
    public List<Users> find() {

        return usersMapper.find();
    }
    //登录
    @Override
    public Users logintwo(String phone, String account,String password) {
        return usersMapper.logintwo(phone,account,password);
    }
    //注册
    @Override
    public void registertwo(String phone, String password) {
        usersMapper.registertwo(phone,password);
    }
    //修改密码
    @Override
    public int updatepassword(String phone, String newpassword) {
        return usersMapper.updatepassword(phone,newpassword);
    }



    //2023-11-21 查询管理员
    @Override
    public List<Users> findAdmin(String name) {
        return usersMapper.findAdmin(name);
    }
    //2023-11-21 添加管理员
    @Override
    public void addAdmin(Users admin) {
        usersMapper.addAdmin(admin);
    }
    //2023-11-21 删除管理员
    @Override
    public void deleteAdmin(Integer uid) {
        usersMapper.deleteAdmin(uid);
    }


    //新写的
//从普通用户成为作者
    @Override
    public void updatePermissionszz(String name) {
        usersMapper.updatePermissionszz(name);
    }
    //从普通用户到待审核
    @Override
    public void UpdatePermissionsdsh(String name) {
        usersMapper.UpdatePermissionsdsh(name);
    }
    //从普通用户到封禁
    @Override
    public void UpdatePermissionsdfj(String name,String uid) {
        usersMapper.UpdatePermissionsdfj(name);
        String key = "uid"+uid;
        RedisTemplate.opsForValue().set(key,uid,180, TimeUnit.DAYS);
    }
    //从封禁到用户到普通用户
    @Override
    public void updatePermissionspt(String name,String uid) {
        usersMapper.updatePermissionspt(name);
        String key = "uid"+uid;
        RedisTemplate.delete(key);
    }

    //2023-11-23查询所有待审核的用户
    @Override
    public List<Users> selectdsh() {
        return usersMapper.selectdsh();
    }

}
